/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.thinkgem.jeesite.modules.cms.web;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.thinkgem.jeesite.common.config.Global;
import com.thinkgem.jeesite.common.persistence.JsonResult;
import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.web.BaseController;
import com.thinkgem.jeesite.common.utils.StringUtils;
import com.thinkgem.jeesite.modules.cms.entity.DataType;
import com.thinkgem.jeesite.modules.cms.service.DataTypeService;

/**
 * 键值对Controller
 * @author leo
 * @version 2017-03-01
 */
@Controller
@RequestMapping(value = "${adminPath}/cms/dataType")
public class DataTypeController extends BaseController {

	@Autowired
	private DataTypeService dataTypeService;
	
	@ModelAttribute
	public DataType get(@RequestParam(required=false) String id) {
		DataType entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = dataTypeService.get(id);
		}
		if (entity == null){
			entity = new DataType();
		}
		return entity;
	}
	
	@RequiresPermissions("cms:dataType:view")
	@RequestMapping(value = {"list", ""})
	public String list(DataType dataType, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<DataType> page = dataTypeService.findPage(new Page<DataType>(request, response), dataType); 
		model.addAttribute("page", page);
		return "modules/cms/dataTypeList";
	}

	@RequiresPermissions("cms:dataType:view")
	@RequestMapping(value = "form")
	public String form(DataType dataType, Model model) {
		model.addAttribute("dataType", dataType);
		return "modules/cms/dataTypeForm";
	}

	@RequiresPermissions("cms:dataType:edit")
	@RequestMapping(value = "save")
	public String save(DataType dataType, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, dataType)){
			return form(dataType, model);
		}
		dataTypeService.save(dataType);
		addMessage(redirectAttributes, "保存信息成功");
		return "redirect:"+Global.getAdminPath()+"/cms/dataType/?repage";
	}
	
	@RequiresPermissions("cms:dataType:edit")
	@RequestMapping(value = "delete")
	public String delete(DataType dataType, RedirectAttributes redirectAttributes) {
		dataTypeService.delete(dataType);
		addMessage(redirectAttributes, "删除信息成功");
		return "redirect:"+Global.getAdminPath()+"/cms/dataType/?repage";
	}
	
	@RequestMapping(value = {"getByType"})
	@ResponseBody
	public JsonResult getByType(String key) {
		JsonResult result = new JsonResult();
		List<DataType> data = dataTypeService.getByType(key); 
		if(data != null && data.size()>0) {
			result.setRet_code(0);
			result.setData(data);
		}else{
			result.setRet_code(1);
			result.setErr_msg("没有数据");
		}
		return result;
	}
	

}